package net.csdn.business.common.i18n;

import net.csdn.business.common.constant.CommonConstants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * 国际化配置
 *
 * @author wanghang
 * @version 1.0
 * @date 2022/9/19 11:11
 */
public class LocaleResolverConfig implements LocaleResolver {

    private static final String PATH_PARAMETER = "lang";

    private static final String PATH_PARAMETER_SPLIT = "_";

    /**
     * 解析国际化配置
     * ps：从header中获取配置
     *
     * @param request
     * @return
     */
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String lang = request.getHeader(PATH_PARAMETER);
        if (StringUtils.isNotBlank(lang)) {
            String[] split = lang.split(PATH_PARAMETER_SPLIT);
            Locale locale = new Locale(split[CommonConstants.INT_ZERO], split[CommonConstants.INT_ONE]);
            return locale;
        }
        return Locale.SIMPLIFIED_CHINESE;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
    }

}
